﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Data.Linq.Mapping;
using System.Data.Linq;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace SMSAssistant.Model
{
    [Table(Name = "message")]
    [DataContract]
    public class Message : NotificationObject
    {
        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        [IgnoreDataMember]
        public int Id { get; set; }

        string title;
        [Column]
        [DataMember]
        public string Title
        {
            get { return title; }
            set
            {
                NotifyPropertyChanging("Title");
                title = value;
                NotifyPropertyChanged("Title");
            }
        }

        string content;
        [Column]
        [DataMember]
        public string Content
        {
            get { return content; }
            set
            {
                NotifyPropertyChanging("Content");
                content = value;
                NotifyPropertyChanged("Content");
            }
        }

        DateTime? createTime;
        [Column]
        [DataMember]
        public DateTime? CreateTime
        {
            get { return createTime; }
            set
            {
                NotifyPropertyChanging("CreateTime");
                createTime = value;
                NotifyPropertyChanged("CreateTime");
            }
        }


        DateTime? receiveTime;
        [Column]
        [DataMember]
        public DateTime? ReceiveTime
        {
            get
            {
                return receiveTime;
            }
            set
            {
                NotifyPropertyChanging("LastReviewTime");
                receiveTime = value;
                NotifyPropertyChanged("LastReviewTime");
            }
        }


        DateTime? lastReviewTime;
        [Column]
        [DataMember]
        public DateTime? LastReviewTime
        {
            get { return lastReviewTime; }
            set
            {
                NotifyPropertyChanging("LastReviewTime");
                lastReviewTime = value;
                NotifyPropertyChanged("LastReviewTime");
            }
        }


        Category category;
        [Column]
        [DataMember]
        public Category Category
        {
            get { return category; }
            set
            {
                NotifyPropertyChanging("Category");
                category = value;
                NotifyPropertyChanged("Category");
            }
        }

        [Column]
        internal int? _contactId;

        private EntityRef<Contact> contact;

        [Association(Storage = "contact", ThisKey = "_contactId", OtherKey = "Id", IsForeignKey = true)]
        [DataMember]
        public Contact Contact
        {
            get { return contact.Entity; }
            set
            {
                NotifyPropertyChanging("Contact");
                contact.Entity = value;
                if (value != null)
                {
                    _contactId = value.Id;
                }
                NotifyPropertyChanged("Contact");
            }
        }

        [Column(IsVersion = true)]
        private Binary _version;

        public bool Match(string text)
        {
            bool result = false;
            if (!string.IsNullOrEmpty(text))
            {
                if (Title.Contains(text) || Content.Contains(text) || (Contact != null && (Contact.DisplayName.Contains(text) || Contact.PhoneNumber.Contains(text))))
                {
                    result = true;
                }
            }
            else
                result = true;
            return result;
        }
        
        public Message Clone()
        {
            var msg = new Message
            {
                Content = this.content,
                CreateTime = DateTime.UtcNow,
                Contact = this.Contact,
                Title = this.title,
                ReceiveTime = this.receiveTime,
                LastReviewTime = DateTime.UtcNow
            };

            return msg;
        }
    }

    public enum Category
    {
        IN,
        OUT
    }
}
